<?php
namespace app\common\job;

use think\Db;
use think\queue\Job;

class Member
{

    /**
     * 会员信息导入处理
     */
    public function importMember(Job $job, $data)
    {
        Db::startTrans();
        try {
            \think\Log::write('queue-' . get_class() . '-importMember' . '：开始执行');
//            $jsonData = json_encode($data);
//            \think\Log::write("具体执行任务接受到的参数：{$jsonData}");

            if(empty($data['name']) || empty($data['mobile']) || empty($data['birthplace']) || empty($data['company_post']) || empty($data['cat_id']) || empty($data['company_name']) || empty($data['company_address'])) {
                // 删除 job
                $job->delete();
                throw new \Exception('必填数据不完整:'. json_encode($data));
            }

            $data['gender'] = $data['gender']=='男'?1:0;

            //添加用户
            $user = model('app\common\model\User')->get(['mobile' => $data['mobile']]);
            if(!$user){
                $Auth = new \app\common\library\Auth;
                $Auth->register($data['mobile'],'123456','',$data['mobile'],['nickname' => $data['name'],'gender' => $data['gender']]);
                $user_id = $Auth->id;
            }else{
                $user_id = $user->id;
            }

            //添加会员
            $member = model('app\admin\model\wdsxh\Member');
            $member->uid = $user_id;
            $member->picture = '/assets/addons/wdsxh/img/default.png';
            $member['name'] = $data['name'];
            $member->mobile = $data['mobile'];
            $member->birthplace = $data['birthplace'];
            if(!empty($data['commerce_id'])){
                $commerce = model('app\admin\model\PrefecturalChamberOfCommerce')->get(['title' => $data['commerce_id']]);
                $member->commerce_id = $commerce?$commerce->id:0;
            }else{
                $member->commerce_id = $data['commerce_id']??0;
            }
            $level_id = 0;
            if(!empty($data['level_id'])){
                $level = model('app\admin\model\wdsxh\MemberUnitLevel')->get(['name' => $data['level_id']]);
                $level_id = $level?$level->id:0;
            }
            $member->level_id = $level_id;
            $member->company_post = $data['company_post'];
            $member->introduction = $data['introduction'];

            $member->gender = $data['gender'];
            $member->political_status = $data['political_status'];

            $member->save();

            //添加企业数据
            $unit = model('app\admin\model\wdsxh\MemberUnit');
            $cat_id = 0;
            if(!empty($data['cat_id'])){
                $cat = model('app\admin\model\wdsxh\MemberUnitCat')->get(['name' => $data['cat_id']]);
                $cat_id = $cat?$cat->id:0;
            }
            $unit->cat_id = $cat_id;
            $unit->level_id = $level_id;
            $unit->member_id = $member->id;
            $unit->company_name = $data['company_name'];
            $unit->company_introduce = $data['company_introduce'];
            if(!empty($data['company_address'])) {
                $company_address_detail = str_replace(' ','',$data['company_address']);
                $unit->company_address_detail = $company_address_detail;
                $lat_lng = getTXLatLng($company_address_detail);
                if(!empty($lat_lng)){
                    $company_address_detail = str_replace($lat_lng['address_components']['province'],'',$company_address_detail);
                    $company_address_detail = str_replace($lat_lng['address_components']['city'],'',$company_address_detail);
                    $company_address_detail = str_replace($lat_lng['address_components']['district'],'',$company_address_detail);
                    $company_address = "{$lat_lng['address_components']['province']}/{$lat_lng['address_components']['city']}/{$lat_lng['address_components']['district']}";
                    $company_address_lat = $lat_lng['location']['lat'];
                    $company_address_lng = $lat_lng['location']['lng'];
                    $unit->company_address = $company_address;
                    $unit->company_address_detail = $company_address_detail;
                    $unit->company_address_lat = $company_address_lat;
                    $unit->company_address_lng = $company_address_lng;
                }
            }
            $unit->save();

            //添加入会申请数据
            $create_member_profile_data = [
                'uid' => $user_id,
                'name' => $data['name'],
                'mobile' => $data['mobile'],
                'status' => '1',
                'createtime' => time(),
            ];
            (new \app\admin\model\wdsxh\MemberProfile())->allowField(true)->save($create_member_profile_data);

            //添加订单信息
            $money = (new \app\admin\model\wdsxh\MemberUnitLevel())->where('id',$level_id)->value('expense');
            $orderData=array(
                'uid' => $user_id,
                'items_id' => $member->id,
                'type' => 2,
                'order_sn' => wdsxh_create_order(),
                'money' => $money??0,
                'status' => '5',
                'createtime' => time(),
                'paytime' => time(),
            );
            model('app\admin\model\wdsxh\Order')->create($orderData);

            Db::commit();

            \think\Log::write('queue-' . get_class() . '-importMember' . '：执行成功');
            // 删除 job
            $job->delete();
        } catch (\Exception $e) {
            Db::rollback();
            // 队列执行失败
            \think\Log::write('queue-' . get_class() . '-importMember' . '：执行失败，错误信息：' . $e->getMessage() . ';Line: ' . $e->getLine());
        }
    }

}